import random
import time
from datetime import datetime
import base64


def seconds_to_datetime(dt: datetime):
    seconds = int(dt.timestamp()) - int(datetime.now().timestamp())
    return seconds

def f_now_add_time(day):
    d1 = (datetime.datetime.now() + datetime.timedelta(days=day)).strftime("%Y-%m-%d %H:%M:%S")
    return d1

def gen_kvstr(data):
        '''
        生成key1=value2&key2=value2格式字符串
        '''
        kv_str = ''
        for k in sorted(data):
            kv_str += k + '=' + str(data[k]) + '&'
        kv_str = kv_str[:-1]
        return kv_str

def base64_encode(content):
    print('>> base64_encode')
    b64_byte = base64.b64encode(content.encode())
    #print(b64_byte)
    return b64_byte.decode()

def base64_decode(b64_str):
    print('>> base64_decode')
    b64_byte = b64_str.encode()
    #print(b64_byte)
    return base64.decodebytes(b64_byte)


def get_date_today():
    date_today = datetime.now().strftime("%Y%m%d")
    return date_today


def get_date_month():
    date_month = datetime.now().strftime("%Y%m")
    return date_month


def get_timestamp(length=13):
    ts = None
    if str(length) == '10':
        ts = int(time.time())
    elif str(length) == '13':
        ts = int(time.time() * 1000)
    else:
        ts = time.time()
    return ts

def get_timestamp_seconds(dt: datetime = None):
    if dt:
        return int(dt.timestamp())
    else:
        return int(datetime.now().timestamp())

def get_time_now(type=1, format=None):
    '''
    eg: yyyy-MM-dd HH:mm:ss -> %Y-%m-%d %H:%M:%S
    type = 1: %Y-%m-%d %H:M:%S
    type = 2: %Y%m%d%H%M%S
    type = 3: %Y%m%d%H%M%S
    '''
    return fmt_time(datetime.now(), type=type, format=format)

def fmt_time(dt:datetime, type=1, format=None):
    '''
    eg: yyyy-MM-dd HH:mm:ss -> %Y-%m-%d %H:%M:%S
    type = 1: %Y-%m-%d %H:M:%S
    type = 2: %Y%m%d%H%M%S
    type = 3: %Y%m%d%H%M%S
    '''
    if format:
        time_format = format
    else:
        if type == 1:
            time_format = "%Y-%m-%d %H:%M:%S"
        elif type == 2:
            time_format = "%Y%m%d%H%M%S"
        elif type == 3:
            time_format = "%Y%m%d%H%M%S%f"
        elif type == 4:
            time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        elif type == 5:
            time_format = "%Y%m%d"
        elif type == 6:
            time_format = "%Y%m"
        else:
            time_format = "%Y-%m-%d %H:%M:%S"
    #print('Time Format: %s' % time_format, 'debug')
    tn = dt.strftime(time_format)
    return tn

def get_time_now_org(type=1, format=None):
    '''
    eg: yyyy-MM-dd HH:mm:ss -> %Y-%m-%d %H:%M:%S
    type = 1: %Y-%m-%d %H:M:%S
    type = 2: %Y%m%d%H%M%S
    type = 3: %Y%m%d%H%M%S
    '''
    if format:
        time_format = format
    else:
        if type == 1:
            time_format = "%Y-%m-%d %H:%M:%S"
        elif type == 2:
            time_format = "%Y%m%d%H%M%S"
        elif type == 3:
            time_format = "%Y%m%d%H%M%S%f"
        elif type == 4:
            time_format = "%Y%m%d"
        else:
            time_format = "%Y-%m-%d %H:%M:%S"
    #print('Time Format: %s' % time_format, 'debug')
    tn = datetime.now().strftime(time_format)
    return tn

def get_time_now_bak():
    tn = datetime.now().strftime("%Y%m%d%H%M%S")
    return tn


def rand_phone_no():
    phone_pre = '13'
    # rand_1st = ''.join(random.sample(['0','1','2','3','4','5','6','7','8','9'], 8))
    rand_no = ''.join(random.sample(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 9))
    phone = phone_pre + rand_no
    print("random phone: %s" % phone)
    return phone


def rand_bank_no():
    cardNO = '62' + '%013d' % (random.randint(1, 9999999999999))
    EvenSum = 0
    OddSum = 0
    cardNOList = list(cardNO)
    # print(cardNOList)
    for i in range(len(cardNOList)):
        k = 0
        if i % 2 == 0:
            # print(cardNOList[(len(cardNOList)-1)-i])
            k = int(cardNOList[(len(cardNOList) - 1) - i]) * 2
            if int(k / 10):
                kk = k - 9
            else:
                kk = k
            EvenSum = EvenSum + kk
        else:
            OddSum = OddSum + int(cardNOList[(len(cardNOList) - 1) - i])
    checkNO_1 = (EvenSum + OddSum) % 10
    if checkNO_1 == 0:
        checkNO = checkNO_1
    else:
        checkNO = 10 - checkNO_1
    bankCardNO = cardNO + str(checkNO)
    return bankCardNO


def regiun():
    '''生成身份证前六位'''
    first_list = ['362402', '362421', '362422',
                  '362423', '362424', '362425',
                  '362426', '362427', '362428',
                  '362429', '362430', '362432',
                  '110100', '110101', '110102',
                  '110103', '110104', '110105',
                  '110106', '110107', '110108',
                  '110109', '110111']
    first = random.choice(first_list)
    return first


def year():
    '''生成年份'''
    now = time.strftime('%Y')
    # 1948为第一代身份证执行年份,now-18直接过滤掉小于18岁出生的年份
    second = random.randint(1948, int(now) - 18)
    age = int(now) - second
    return second


def month():
    '''生成月份'''
    three = random.randint(1, 12)
    # 月份小于10以下，前面加上0填充
    if three < 10:
        three = '0' + str(three)
        return three
    else:
        return three


def day():
    '''生成日期'''
    four = random.randint(1, 31)
    # 日期小于10以下，前面加上0填充
    if four < 10:
        four = '0' + str(four)
        return four
    else:
        return four


def randoms():
    '''生成身份证后四位'''
    # 后面序号低于相应位数，前面加上0填充
    five = random.randint(1, 9999)
    if five < 10:
        five = '000' + str(five)
        return five
    elif 10 < five < 100:
        five = '00' + str(five)
        return five
    elif 100 < five < 1000:
        five = '0' + str(five)
        return five
    else:
        return five


def rand_id_card():
    first = regiun()
    second = year()
    three = month()
    four = day()
    last = randoms()
    IDcard = str(first) + str(second) + str(three) + str(four) + str(last)
    return IDcard


if __name__ == "__main__":
    phone = rand_phone_no()
    print(phone)
    print(get_time_now())
    print(rand_order_no())
    print(rand_bank_no())
    print(rand_id_card())
